package com.jett.mycloud.resp;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author : 王雪峰
 * @Date : 2024/3/1 15:11
 */
@Data
@Accessors(chain = true)
@Slf4j
public class ResultData<T> {
    private String code;
    private String message;
    private T data;
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",timezone = "GMT+8")
    private long timestamp;

    public ResultData() {
        this.timestamp=System.currentTimeMillis();
    }


    public static <T> ResultData<T> success(T data){
        ResultData resultData = new ResultData<>();
        resultData.setCode(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(ReturnCodeEnum.RC200.getMessage());
        resultData.setData(data);
        return  resultData;
    }
    public static <T> ResultData<T> fail(String code,String message){
        ResultData resultData = new ResultData<>();
        resultData.setCode(ReturnCodeEnum.getReturnCodeEnum(code).getCode());
        if ( message ==null){
            resultData.setMessage(ReturnCodeEnum.getReturnCodeEnum(code).getMessage());
        }else{
            resultData.setMessage(message);
        }
        return  resultData;
    }
}
